package com.coursehub.exercise.controller;

import com.coursehub.exercise.entity.FavoriteQuestionBank;
import com.coursehub.exercise.service.FavoriteQuestionBankService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/exercises/favorite-banks")
@RequiredArgsConstructor
public class FavoriteQuestionBankController {

    private final FavoriteQuestionBankService favoriteQuestionBankService;

    @PostMapping
    public ResponseEntity<FavoriteQuestionBank> create(@RequestBody Map<String, String> body) {
        FavoriteQuestionBank bank = favoriteQuestionBankService.create(
                body.get("favoriteQuestionBankId"),
                body.get("userId"),
                body.get("courseId"));
        return ResponseEntity.ok(bank);
    }

    @DeleteMapping("/{favoriteQuestionBankId}")
    public ResponseEntity<Void> delete(@PathVariable String favoriteQuestionBankId) {
        favoriteQuestionBankService.delete(favoriteQuestionBankId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<FavoriteQuestionBank>> listByUser(@PathVariable String userId) {
        return ResponseEntity.ok(favoriteQuestionBankService.listByUser(userId));
    }

    @GetMapping("/user/{userId}/course/{courseId}")
    public ResponseEntity<List<FavoriteQuestionBank>> listByUserAndCourse(@PathVariable String userId, @PathVariable String courseId) {
        return ResponseEntity.ok(favoriteQuestionBankService.listByUserAndCourse(userId, courseId));
    }

    // relation with questions
    @PostMapping("/{favoriteQuestionBankId}/questions/{questionId}")
    public ResponseEntity<Void> addQuestion(@PathVariable String favoriteQuestionBankId, @PathVariable String questionId) {
        favoriteQuestionBankService.addQuestion(favoriteQuestionBankId, questionId);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{favoriteQuestionBankId}/questions/{questionId}")
    public ResponseEntity<Void> removeQuestion(@PathVariable String favoriteQuestionBankId, @PathVariable String questionId) {
        favoriteQuestionBankService.removeQuestion(favoriteQuestionBankId, questionId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{favoriteQuestionBankId}/questions")
    public ResponseEntity<List<String>> listQuestions(@PathVariable String favoriteQuestionBankId) {
        return ResponseEntity.ok(favoriteQuestionBankService.listQuestionIds(favoriteQuestionBankId));
    }
}


